//
//  Opration.swift
//  swift_review
//
//  Created by Andrew on 2025/1/8.
//

import UIKit

class Opration: NSObject {
    
    var medies: [String] = ["", "", "", "", ""]
    
    func getDict(path: String, isLog: Bool, medies: [String], completion: @escaping ([String]) -> Void) {
        var address: [String] = []
        let dispatchGroup = DispatchGroup() // 创建一个 DispatchGroup
        
        for str in medies {
            switch str {
            case "image":
                dispatchGroup.enter() // 进入组
                self.uploadImage(image: UIImage(named: str)!) { url in
                    address.append(url)
                    dispatchGroup.leave() // 完成后离开组
                }
                break
            case "video":
                dispatchGroup.enter() // 进入组
                self.uploadVideo(video: str) { url in
                    address.append(url)
                    dispatchGroup.leave() // 完成后离开组
                }
                break
            default:
                break
            }
        }
        
        if isLog {
            dispatchGroup.enter() // 进入组
            self.uploadLog(logPath: path) { url in
                address.append(url)
                dispatchGroup.leave() // 完成后离开组
            }
        }
        
        // 使用 dispatchGroup.notify 来等待所有异步任务完成
        dispatchGroup.notify(queue: .main) {
            completion(address) // 在所有任务完成后回调
        }
    }
}

extension Opration {
    func uploadImage(image: UIImage, completion: @escaping (String) -> Void) {
        
    }
    
    func uploadVideo(video: String, completion: @escaping (String) -> Void) {
        
    }
    
    func uploadLog(logPath: String, completion: @escaping (String) -> Void) {
        
    }
}

extension Opration {
    func getDictSemaphore(path: String, isLog: Bool, medies: [String], completion: @escaping ([String]) -> Void) {
        var address: [String] = []
        let semaphore = DispatchSemaphore(value: 0) // 创建信号量，初始值为0
        
        for str in medies {
            switch str {
            case "image":
                self.uploadImage(image: UIImage(named: str)!) { url in
                    address.append(url)
                    semaphore.signal() // 完成任务后释放信号
                }
            case "video":
                self.uploadVideo(video: str) { url in
                    address.append(url)
                    semaphore.signal() // 完成任务后释放信号
                }
            default:
                break
            }
        }
        
        if isLog {
            self.uploadLog(logPath: path) { url in
                address.append(url)
                semaphore.signal() // 完成任务后释放信号
            }
        }
        
        // 等待所有任务完成
        for _ in medies {
            semaphore.wait() // 等待任务完成
        }
        
        // 等待上传日志的任务
        if isLog {
            semaphore.wait()
        }
        
        // 所有任务完成后回调
        completion(address)
    }
}

extension Opration {
    func getDictOperatioQueue(path: String, isLog: Bool, medies: [String], completion: @escaping ([String]) -> Void) {
        var address: [String] = []
        let queue = OperationQueue()
        queue.maxConcurrentOperationCount = 1 // 确保任务按顺序执行（如果需要并发执行可以去掉这行）
        
        for str in medies {
            switch str {
            case "image":
                let operation = BlockOperation {
                    self.uploadImage(image: UIImage(named: str)!) { url in
                        address.append(url)
                    }
                }
                queue.addOperation(operation)
            case "video":
                let operation = BlockOperation {
                    self.uploadVideo(video: str) { url in
                        address.append(url)
                    }
                }
                queue.addOperation(operation)
            default:
                break
            }
        }
        
        if isLog {
            let operation = BlockOperation {
                self.uploadLog(logPath: path) { url in
                    address.append(url)
                }
            }
            queue.addOperation(operation)
        }
        
        // 等待队列中所有任务完成后回调
        queue.addOperation {
            completion(address)
        }
    }
    
}

//extension Opration {
//    func getDictAsync(path: String, isLog: Bool, medies: [String]) async -> [String] {
//        var address: [String] = []
//        
//        // 异步任务
//        for str in medies {
//            switch str {
//            case "image":
//                let url = await uploadImage(image: UIImage(named: str)!)
//                address.append(url)
//            case "video":
//                let url = await uploadVideo(video: str)
//                address.append(url)
//            default:
//                break
//            }
//        }
//        
//        if isLog {
//            let url = await uploadLog(logPath: path)
//            address.append(url)
//        }
//        
//        return address
//    }
//
//    // 调用方法
//    Task {
//        let result = await getDict(path: "somePath", isLog: true, medies: ["image", "video"])
//        print(result)
//    }
//}
